/*
 * @Author: duchengdong
 * @Date: 2021-12-28 18:10:22
 * @LastEditors: lizhen
 * @LastEditTime: 2021-12-31 18:37:25
 * @Description:
 */
const tempId = '0JlylkobguT6TVY5MAVtt4nUz53sCsgAFZFAGX4e0Ds'
const subscribeMessage = {
	methods: {
		async getSetting() {
			let res = await uni.getSetting({withSubscriptions: true})
			console.log(res, 'getSetting1')
			if (res[1] && res[1].errMsg == 'getSetting:ok') {
				let subscriptionsSetting = res[1].subscriptionsSetting
				if (subscriptionsSetting.mainSwitch == false || (subscriptionsSetting.itemSettings && subscriptionsSetting.itemSettings[tempId] != 'accept')) {
					let operateRes = await this.openSetting()
					console.log(operateRes, 'operateRes')
					return false
				}
				return true
			} else {
				return false
			}
		},
		async openSetting() {
			let res = await uni.openSetting({withSubscriptions: true})
			console.log(res, 'openSetting')
			return res
		},
		async subscribeHandle() {
			let subscribeMessageStatus = await this.getSetting()
			console.log(subscribeMessageStatus, 'subscribeMessageStatus')
			if (subscribeMessageStatus) {
				let tmplIds = [tempId]
				let res = await uni.requestSubscribeMessage({
					tmplIds: tmplIds,
				})
				console.log(res, 'subscribeMessage')
				if (res[0] != null) {
					return 'reject'
				}
				if (res[0] == null && res[1].errMsg == 'requestSubscribeMessage:ok') {
					return res[1][tempId]
				} else {
					return 'reject'
				}
			}
			return 'reject'
			/*
            {
                0JlylkobguT6TVY5MAVtt4nUz53sCsgAFZFAGX4e0Ds: "reject"
                errMsg: "requestSubscribeMessage:ok"
            }
            {
                0JlylkobguT6TVY5MAVtt4nUz53sCsgAFZFAGX4e0Ds: "accept"
                errMsg: "requestSubscribeMessage:ok"
            }
            */
		},
	},
}
export default subscribeMessage
